#!/bin/bash
# 源码和编译输出目录变量
SCRIPT_FFMPEG=~/ffmpeg_sources/script_ffmpeg
SRC_DIR=~/ffmpeg_sources
BUILD_DIR=~/ffmpeg_build
BIN_DIR=~/bin
# COMPILE_TOOL_CHAIN="arm-ca7-linux-uclibcgnueabihf"
COMPILE_TOOL_CHAIN="arm-ca7-linux-uclibcgnueabihf"

# 控制是否解压和编译各源码包（1=解压编译，0=跳过）
BUILD_NASM=0
BUILD_YASM=0
BUILD_X264=0
BUILD_X265=0
BUILD_VPX=0
BUILD_FDK_AAC=0
BUILD_LAME=0
BUILD_OPUS=0
BUILD_AOM=0
BUILD_ZLIB=0

ERROR_COUNT=0
SUCCESS_COUNT=0

# 支持参数控制
if [ $# -ge 1 ]; then
    for arg in "$@"; do
        case "$arg" in
            all)
                BUILD_NASM=1
                BUILD_YASM=1
                BUILD_X264=1
                BUILD_X265=1
                BUILD_VPX=1
                BUILD_FDK_AAC=1
                BUILD_LAME=1
                BUILD_OPUS=1
                BUILD_AOM=1
                BUILD_ZLIB=1
                ;;
            nasm) BUILD_NASM=1 ;;
            yasm) BUILD_YASM=1 ;;
            x264) BUILD_X264=1 ;;
            x265) BUILD_X265=1 ;;
            vpx) BUILD_VPX=1 ;;
            fdk-aac) BUILD_FDK_AAC=1 ;;
            lame) BUILD_LAME=1 ;;
            opus) BUILD_OPUS=1 ;;
            aom) BUILD_AOM=1 ;;
            zlib) BUILD_ZLIB=1 ;;
            *)
                echo "未知参数: $arg"
                echo "用法: $0 [all|nasm|yasm|x264|x265|vpx|fdk-aac|lame|opus|aom|zlib] ..."
                exit 1
                ;;
        esac
    done
fi


# 创建工作目录
mkdir -p "$SRC_DIR" "$BUILD_DIR" "$BIN_DIR"

# ...前面内容保持不变...

SUCCESS_LIST=""
FAIL_LIST=""

### 二、安装汇编器 NASM
if [ "$BUILD_NASM" = "1" ]; then
    if [ -f "$SRC_DIR/nasm-2.14.02.tar.bz2" ]; then
        echo "==> 解压并编译 NASM..."
        cd "$SRC_DIR" && \
        tar xjvf nasm-2.14.02.tar.bz2 && \
        cd nasm-2.14.02 && \
        ./autogen.sh && \
        PATH="$BIN_DIR:$PATH" ./configure --prefix="$BUILD_DIR" --bindir="$BIN_DIR" && \
        make && \
        make install
        if [ $? -eq 0 ]; then
            SUCCESS_COUNT=$((SUCCESS_COUNT+1))
            SUCCESS_LIST="${SUCCESS_LIST}nasm-2.14.02.tar.bz2 "
        else
            echo "!! NASM 编译失败"
            ERROR_COUNT=$((ERROR_COUNT+1))
            FAIL_LIST="${FAIL_LIST}nasm-2.14.02.tar.bz2 "
        fi
    else
        echo "!! 缺少文件: nasm-2.14.02.tar.bz2"
        ERROR_COUNT=$((ERROR_COUNT+1))
        FAIL_LIST="${FAIL_LIST}nasm-2.14.02.tar.bz2 "
    fi
else
    echo "==> 跳过 NASM 编译"
fi

### 三、安装汇编器Yasm
if [ "$BUILD_YASM" = "1" ]; then
    if [ -f "$SRC_DIR/yasm-1.3.0.tar.gz" ]; then
        echo "==> 解压并编译 Yasm..."
        cd "$SRC_DIR" && \
        tar xzvf yasm-1.3.0.tar.gz && \
        cd yasm-1.3.0 && \
        ./configure --prefix="$BUILD_DIR" --bindir="$BIN_DIR" && \
        make && \
        make install
        if [ $? -eq 0 ]; then
            SUCCESS_COUNT=$((SUCCESS_COUNT+1))
            SUCCESS_LIST="${SUCCESS_LIST}yasm-1.3.0.tar.gz "
        else
            echo "!! Yasm 编译失败"
            ERROR_COUNT=$((ERROR_COUNT+1))
            FAIL_LIST="${FAIL_LIST}yasm-1.3.0.tar.gz "
        fi
    else
        echo "!! 缺少文件: yasm-1.3.0.tar.gz"
        ERROR_COUNT=$((ERROR_COUNT+1))
        FAIL_LIST="${FAIL_LIST}yasm-1.3.0.tar.gz "
    fi
else
    echo "==> 跳过 Yasm 编译"
fi

### 四、安装H.264编码器libx264
if [ "$BUILD_X264" = "1" ]; then
    if [ -f "$SRC_DIR/x264.tar.gz" ]; then
        echo "==> 解压并编译 x264..."
        # ./configure --prefix=../x264build --disable-asm --enable-shared --enable-static --host=arm-linux-gnueabi --cross-prefix=arm-fsl-linux-gnueabi-

        cd "$SRC_DIR" && \
        tar xzvf x264.tar.gz && \
        cd $(find . -maxdepth 1 -type d -name "x264*" | head -n 1) && \
        PATH="$BIN_DIR:$PATH" PKG_CONFIG_PATH="$BUILD_DIR/lib/pkgconfig" \
        ./configure --prefix="$BUILD_DIR" --bindir="$BIN_DIR" --disable-asm --enable-shared --enable-static --enable-pic \ 
        --host=$COMPILE_TOOL_CHAIN --cross-prefix=$COMPILE_TOOL_CHAIN- && \
        PATH="$BIN_DIR:$PATH" make && \
        make install
        if [ $? -eq 0 ]; then
            SUCCESS_COUNT=$((SUCCESS_COUNT+1))
            SUCCESS_LIST="${SUCCESS_LIST}x264.tar.gz "
        else
            echo "!! x264 编译失败"
            ERROR_COUNT=$((ERROR_COUNT+1))
            FAIL_LIST="${FAIL_LIST}x264.tar.gz "
        fi
    else
        echo "!! 缺少文件: x264.tar.gz"
        ERROR_COUNT=$((ERROR_COUNT+1))
        FAIL_LIST="${FAIL_LIST}x264.tar.gz "
    fi
else
    echo "==> 跳过 x264 编译"
fi

### 五、安装H.265编码器libx265
if [ "$BUILD_X265" = "1" ]; then
    if [ -f "$SRC_DIR/x265.tar.gz" ]; then
        echo "==> 解压并编译 x265..."
        cd "$SRC_DIR" && \
        tar xzvf x265.tar.gz && \
        cd x265/build/linux && \
        PATH="$BIN_DIR:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$BUILD_DIR" -DENABLE_SHARED=off ../../source && \
        PATH="$BIN_DIR:$PATH" make && \
        make install
        if [ $? -eq 0 ]; then
            SUCCESS_COUNT=$((SUCCESS_COUNT+1))
            SUCCESS_LIST="${SUCCESS_LIST}x265.tar.gz "
        else
            echo "!! x265 编译失败"
            ERROR_COUNT=$((ERROR_COUNT+1))
            FAIL_LIST="${FAIL_LIST}x265.tar.gz "
        fi
    else
        echo "!! 缺少文件: x265.tar.gz"
        ERROR_COUNT=$((ERROR_COUNT+1))
        FAIL_LIST="${FAIL_LIST}x265.tar.gz "
    fi
else
    echo "==> 跳过 x265 编译"
fi

### 六、安装VP8/VP9编解码器libvpx
if [ "$BUILD_VPX" = "1" ]; then
    if [ -f "$SRC_DIR/libvpx.tar.gz" ]; then
        echo "==> 解压并编译 libvpx..."
        cd "$SRC_DIR" && \
        tar xzvf libvpx.tar.gz && \
        cd libvpx && \
        PATH="$BIN_DIR:$PATH" ./configure --prefix="$BUILD_DIR" --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=yasm --enable-pic && \
        PATH="$BIN_DIR:$PATH" make && \
        make install
        if [ $? -eq 0 ]; then
            SUCCESS_COUNT=$((SUCCESS_COUNT+1))
            SUCCESS_LIST="${SUCCESS_LIST}libvpx.tar.gz "
        else
            echo "!! libvpx 编译失败"
            ERROR_COUNT=$((ERROR_COUNT+1))
            FAIL_LIST="${FAIL_LIST}libvpx.tar.gz "
        fi
    else
        echo "!! 缺少文件: libvpx.tar.gz"
        ERROR_COUNT=$((ERROR_COUNT+1))
        FAIL_LIST="${FAIL_LIST}libvpx.tar.gz "
    fi
else
    echo "==> 跳过 libvpx 编译"
fi

### 七、安装AAC编码器libfdk-aac
if [ "$BUILD_FDK_AAC" = "1" ]; then
    if [ -f "$SRC_DIR/fdk-aac.tar.gz" ]; then
        echo "==> 解压并编译 fdk-aac..."
        cd "$SRC_DIR" && \
        tar xzvf fdk-aac.tar.gz && \
        cd fdk-aac && \
        autoreconf -fiv && \
        ./configure CFLAGS=-fPIC --prefix="$BUILD_DIR" && \
        make && \
        make install
        if [ $? -eq 0 ]; then
            SUCCESS_COUNT=$((SUCCESS_COUNT+1))
            SUCCESS_LIST="${SUCCESS_LIST}fdk-aac.tar.gz "
        else
            echo "!! fdk-aac 编译失败"
            ERROR_COUNT=$((ERROR_COUNT+1))
            FAIL_LIST="${FAIL_LIST}fdk-aac.tar.gz "
        fi
    else
        echo "!! 缺少文件: fdk-aac.tar.gz"
        ERROR_COUNT=$((ERROR_COUNT+1))
        FAIL_LIST="${FAIL_LIST}fdk-aac.tar.gz "
    fi
else
    echo "==> 跳过 fdk-aac 编译"
fi

### 八、安装MP3编码器libmp3lame
if [ "$BUILD_LAME" = "1" ]; then
    if [ -f "$SRC_DIR/lame.tar.gz" ]; then
        echo "==> 解压并编译 lame..."
        cd "$SRC_DIR" && \
        tar xzvf lame.tar.gz && \
        cd lame && \
        PATH="$BIN_DIR:$PATH" ./configure --prefix="$BUILD_DIR" --bindir="$BIN_DIR" --enable-nasm --with-pic && \
        PATH="$BIN_DIR:$PATH" make && \
        make install
        if [ $? -eq 0 ]; then
            SUCCESS_COUNT=$((SUCCESS_COUNT+1))
            SUCCESS_LIST="${SUCCESS_LIST}lame.tar.gz "
        else
            echo "!! lame 编译失败"
            ERROR_COUNT=$((ERROR_COUNT+1))
            FAIL_LIST="${FAIL_LIST}lame.tar.gz "
        fi
    else
        echo "!! 缺少文件: lame.tar.gz"
        ERROR_COUNT=$((ERROR_COUNT+1))
        FAIL_LIST="${FAIL_LIST}lame.tar.gz "
    fi
else
    echo "==> 跳过 lame 编译"
fi

### 九、安装Opus编解码器libopus
if [ "$BUILD_OPUS" = "1" ]; then
    if [ -f "$SRC_DIR/opus.tar.gz" ]; then
        echo "==> 解压并编译 opus..."
        cd "$SRC_DIR" && \
        tar xzvf opus.tar.gz && \
        cd opus && \
        ./autogen.sh && \
        ./configure --prefix="$BUILD_DIR" -with-pic && \
        make && \
        make install
        if [ $? -eq 0 ]; then
            SUCCESS_COUNT=$((SUCCESS_COUNT+1))
            SUCCESS_LIST="${SUCCESS_LIST}opus.tar.gz "
        else
            echo "!! opus 编译失败"
            ERROR_COUNT=$((ERROR_COUNT+1))
            FAIL_LIST="${FAIL_LIST}opus.tar.gz "
        fi
    else
        echo "!! 缺少文件: opus.tar.gz"
        ERROR_COUNT=$((ERROR_COUNT+1))
        FAIL_LIST="${FAIL_LIST}opus.tar.gz "
    fi
else
    echo "==> 跳过 opus 编译"
fi

### libaom
if [ "$BUILD_AOM" = "1" ]; then
    if [ -f "$SRC_DIR/aom.tar.gz" ]; then
        echo "==> 解压并编译 aom..."
        cd "$SRC_DIR" && \
        tar xzvf aom.tar.gz && \
        mkdir -p aom_build && \
        cd aom_build && \
        PATH="$BIN_DIR:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$BUILD_DIR" -DENABLE_SHARED=off -DENABLE_NASM=on ../aom && \
        PATH="$BIN_DIR:$PATH" make && \
        make install
        if [ $? -eq 0 ]; then
            SUCCESS_COUNT=$((SUCCESS_COUNT+1))
            SUCCESS_LIST="${SUCCESS_LIST}aom.tar.gz "
        else
            echo "!! aom 编译失败"
            ERROR_COUNT=$((ERROR_COUNT+1))
            FAIL_LIST="${FAIL_LIST}aom.tar.gz "
        fi
    else
        echo "!! 缺少文件: aom.tar.gz"
        ERROR_COUNT=$((ERROR_COUNT+1))
        FAIL_LIST="${FAIL_LIST}aom.tar.gz "
    fi
else
    echo "==> 跳过 aom 编译"
fi

### zlib
ZLIB_PACKAGE_FILE=zlib-1.2.11.tar.gz
if [ "$BUILD_ZLIB" = "1" ]; then
    if [ -f "$SRC_DIR/$ZLIB_PACKAGE_FILE" ]; then
        echo "==> 解压并编译 zlib..."
        cd "$SRC_DIR" && \
        tar xzvf $ZLIB_PACKAGE_FILE && \
        cd $(find . -maxdepth 1 -type d -name "zlib-*" | head -n 1) && \
        sudo ./configure --prefix=$BUILD_DIR \

        #todo makefile replace
        cp -f $SCRIPT_FFMPEG/zlib.arm-ca7-linux-uclibcgnueabihf.Makefile Makefile

        make && \
        make install
        if [ $? -eq 0 ]; then
            SUCCESS_COUNT=$((SUCCESS_COUNT+1))
            SUCCESS_LIST="${SUCCESS_LIST}opus.tar.gz "
        else
            echo "!! opus 编译失败"
            ERROR_COUNT=$((ERROR_COUNT+1))
            FAIL_LIST="${FAIL_LIST}opus.tar.gz "
        fi
    else
        echo "!! 缺少文件: opus.tar.gz"
        ERROR_COUNT=$((ERROR_COUNT+1))
        FAIL_LIST="${FAIL_LIST}opus.tar.gz "
    fi
else
    echo "==> 跳过 opus 编译"
fi

echo "=============================="
echo "编译成功个数: $SUCCESS_COUNT"
echo "成功的文件: $SUCCESS_LIST"
echo "编译或文件错误个数: $ERROR_COUNT"
echo "失败的文件: $FAIL_LIST"
echo "=============================="